package com.rc.saas.tenant.service.enterprise;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.mapper.enterprise.RcEnterpriseGainSettingMapper;
import com.rc.saas.tenant.model.enterprise.RcEnterpriseGainGradeSetting;
import com.rc.saas.tenant.model.enterprise.RcEnterpriseGainGradeSettingExample;
import com.rc.saas.tenant.model.enterprise.RcEnterpriseGainSetting;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RcEnterpriseGainSettingServiceImpl implements RcEnterpriseGainSettingService{

    @Autowired
    private RcEnterpriseGainSettingMapper enterpriseGainSettingMapper;

    @Autowired
    private RcEnterpriseGainGradeSettingService enterpriseGainGradeSettingService;

    @Override
    public RcEnterpriseGainSettingMapper getMapper() {
        return enterpriseGainSettingMapper;
    }

    @Override
    public PageInfo<RcEnterpriseGainSetting> listPageEnterpriseGainSetting(String gainName, Integer pageNo, Integer pageSize) {
        List<RcEnterpriseGainSetting> list = enterpriseGainSettingMapper.listEnterpriseGainSetting(gainName);
        return new PageInfo<>(list);
    }

    @Override
    public void addEnterpriseGainSetting(RcEnterpriseGainSetting enterpriseGainSetting, List<RcEnterpriseGainGradeSetting> grades) {
        enterpriseGainSettingMapper.insertReturnId(enterpriseGainSetting);
        grades.forEach(grade -> {
            grade.setEnterpriseGainId(enterpriseGainSetting.getEnterpriseGainId());
        });
        if (grades != null && grades.size() > 0) {
            enterpriseGainGradeSettingService.batchInsert(grades);
        }

    }

    @Override
    public void updateEnterpriseGainSetting(RcEnterpriseGainSetting enterpriseGainSetting,
                                            List<RcEnterpriseGainGradeSetting> addGradeList,
                                            List<RcEnterpriseGainGradeSetting> editGradeList,
                                            List<Long> delGradeList) {
        if (StringUtils.isEmpty(enterpriseGainSetting.getGainLogo())) {
            enterpriseGainSetting.setGainLogo(null);
        }
        enterpriseGainSettingMapper.updateByPrimaryKeySelective(enterpriseGainSetting);
        //查询历史入库的权益等级列表
        RcEnterpriseGainGradeSettingExample gradeSettingExample = new RcEnterpriseGainGradeSettingExample();
        gradeSettingExample.createCriteria().andEnterpriseGainIdEqualTo(enterpriseGainSetting.getEnterpriseGainId());
        List<RcEnterpriseGainGradeSetting> exsitGradeList = enterpriseGainGradeSettingService.selectByExample(gradeSettingExample);
        if (!CollectionUtils.isEmpty(addGradeList)) {
            addGradeList.forEach(grade -> {
                grade.setEnterpriseGainId(enterpriseGainSetting.getEnterpriseGainId());
            });
            enterpriseGainGradeSettingService.batchInsert(addGradeList);
        }
        if (!CollectionUtils.isEmpty(editGradeList)) {
            //遍历筛查出前端已删除的权益等级列表
            for (RcEnterpriseGainGradeSetting gradeSetting:editGradeList) {
                Iterator<RcEnterpriseGainGradeSetting> iterable = exsitGradeList.iterator();
                while (iterable.hasNext()){
                    RcEnterpriseGainGradeSetting currSet = iterable.next();
                    if(currSet.getGainGradeId().equals(gradeSetting.getGainGradeId())){
                        iterable.remove();
                    }
                }
            }
            enterpriseGainGradeSettingService.batchUpdate(editGradeList);
        }
        if (!CollectionUtils.isEmpty(exsitGradeList)) {
            delGradeList = exsitGradeList.stream().map(e->e.getGainGradeId()).collect(Collectors.toList());
        }
        if (!CollectionUtils.isEmpty(delGradeList)) {
            enterpriseGainGradeSettingService.batchDelete(delGradeList);
        }

    }

    @Override
    public List<RcEnterpriseGainSetting> listGainSettingByLevel(String level) {
        List<RcEnterpriseGainSetting> list = enterpriseGainSettingMapper.listGainSettingByLevel(level);
        return list;
    }

    @Override
    public void deleteGainSetting(Long gainId) {
        List<RcEnterpriseGainGradeSetting> gradeSettingList = enterpriseGainGradeSettingService.listGradeByGain(gainId);
        List<Long> gradeIdList = gradeSettingList.stream().map(RcEnterpriseGainGradeSetting::getGainGradeId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(gradeIdList)) {
            enterpriseGainGradeSettingService.batchDelete(gradeIdList);
        }
        enterpriseGainSettingMapper.deleteByPrimaryKey(gainId);
    }
}
